
<!-- 是templete 不是  templete -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <!-- :to="{ path: '/homePage' }" -->
      <el-breadcrumb-item :to="{ path: '/homePage' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- @clear = "getGoodsList" -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            :clearable="true"
            @input="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" @click="toAddPage"> 添加商品 </el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" :border="true" stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="700"
        ></el-table-column>

        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="180"
        ></el-table-column>

        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="180"
        ></el-table-column>

        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="180"
        ></el-table-column>

        <el-table-column label="添加时间" width="180" prop="add_time">
          <template slot-scope="scope">
            <!-- 2种格式化时间的方法 -->
            <!-- {{formateDateTime(scope.row.add_time)}} -->
            {{ scope.row.add_time | dateFormatFilter }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="openEditDialog(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="removeByGoodId(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 7, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
    
    <script>
export default {
  data() {
    //数据区
    return {
      queryInfo: {
        pagenum: 1,
        pagesize: 10,
        query: "",
      },
      goodsList: [],
      pagenum: 1,
      pagesize: 10,
      total: 0,
    };
  },

  //方法区
  methods: {
    formateDateTime(originalVal) {
      // let date = new Date(originalVal)
      // let y = date.getFullYear() // 年
      // let MM = date.getMonth() + 1 // 月
      // MM = MM < 10 ? ('0' + MM) : MM
      // let d = date.getDate() // 日
      // d = d < 10 ? ('0' + d) : d
      // let h = date.getHours() // 时
      // h = h < 10 ? ('0' + h) : h
      // let m = date.getMinutes()// 分
      // m = m < 10 ? ('0' + m) : m
      // let s = date.getSeconds()// 秒
      // s = s < 10 ? ('0' + s) : s
      // return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s

      const dt = new Date(originalVal);
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDay() + "").padStart(2, "0");
      const h = (dt.getHours() + "").padStart(2, "0");
      const M = (dt.getMinutes() + "").padStart(2, "0");
      const s = (dt.getSeconds() + "").padStart(2, "0");
      return `${y}-${m}-${d} ${h}:${M}:${s}`;
    },

    // 方法1 商品列表
    async getGoodsList() {
      const { data: resData } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (resData.meta.status !== 200) {
        return this.$message.error(resData.meta.msg);
      }
      this.goodsList = resData.data.goods;
      this.pagenum = resData.data.pagenum;
      this.total = resData.data.total;
    },

    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize;
      this.getGoodsList();
    },

    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    openEditDialog(goodsId) {},

    // 方法 删除商品
    async removeByGoodId(goodsId) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      console.log("confirmResult is " + confirmResult);
      if (confirmResult === "cancel") return this.$message.info("已取消删除");

      const { data: resData } = await this.$http.delete(`goods/${goodsId}`);
      if (resData.meta.status !== 200) {
        return this.$message.error(resData.meta.msg)
      }
      this.$message.success(resData.meta.msg);
      this.getGoodsList();
    },
    
    // 跳转到添加商品页面
    toAddPage(){
      // this.$router.push('/goods/add')
      this.$router.push('goods/add')
    }
  },

  //钩子函数
  created() {
    this.getGoodsList();
  },
};
</script>
    
  <style lang="less" scoped>
</style>